﻿@using Fierce.MvcExtend.Controls.Manage

<div title="@ViewBag.Work.Menu.Name" class="easyui-panel" data-options="fit:true,border:false,iconCls:'@ViewBag.Work.Menu.Icon'">
    <table id="urgrid"></table>
    <div id="urtools">@Easyui.Toolbar(ViewBag.Work, true)</div>
</div>

<script type="text/javascript">
    $(function () {
        //表格初始化
        var gridInit = function () {
            $("#urgrid").datagrid({
                url: "/basic/userrole/grid",
                nowrap: false,
                border: false,
                fit: true,
                singleSelect: true,
                loadMsg: "正在处理...",
                idField: "ID",
                toolbar: "#urtools",
                columns: [[
                    {
                        field: "RoleID", title: "角色", width: 228, align: "center",
                        formatter: function (value, row, index) {
                            if (row.BasicRole) { return row.BasicRole.Name; }
                        }
                    }
                ]]
            });
        }();
        //添加
        var insert = function () {
            var row = $("#ugrid").datagrid("getSelected");
            if (row) {
                mutual("添加", null, row.ID);
            }
            else { manage.slide("请选择用户"); }
        };
        //修改
        var update = function () {
            var row = $("#urgrid").datagrid("getSelected");
            if (row) {
                mutual("修改", row.ID, null);
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //删除
        var remove = function () {
            var row = $("#urgrid").datagrid("getSelected");
            if (row) {
                manage.confirm("确认删除?", function (del) {
                    if (del) {
                        manage.ajax({
                            url: "/basic/userrole/remove",
                            type: "post", data: { id: row.ID },
                            success: function (result) {
                                manage.slide(result.message);
                                if (result.success) { reload(); }
                            }
                        });
                    }
                });
            }
            else { manage.slide(manage.nullRowMsg); }
        };
        //实体操作
        var mutual = function (title, id, userid) {
            var href = "/basic/userrole/page?" + $.param({ id: id, userid: userid });
            var d = manage.dialog({
                title: title, href: href, width: 640,
                handler: function () {
                    var rolerow = $("#urrgrid").datagrid("getSelected");
                    if (rolerow) {
                        $("#roleid").val(rolerow.ID);
                    }
                    else {
                        manage.msg("请选择赋予角色"); return;
                    }
                    manage.form({
                        dialog: d, formid: "#urform",
                        url: "/basic/userrole/save",
                        success: function (result) { reload(); }
                    });
                },
                onLoad: function () { formInit(); }
            });
        };
        //弹窗初始化
        var formInit = function () {
            $("#urrgrid").datagrid({
                url: "/basic/role/allgrid",
                nowrap: false, rownumbers: true, title: "选择角色",
                fit: true, singleSelect: true, idField: "ID",
                columns:
                    [[
                        { field: "Name", title: "名称", width: 140, align: "center" },
                        { field: "Remark", title: "描述", width: 325, align: "center" },
                        {
                            field: "IsEnabled", title: "启用", width: 40, align: "center",
                            formatter: function (value, row, index) { return manage.iconYesOrNo(value); }
                        }
                    ]],
                onLoadSuccess: function (data) {
                    var roleid = $("#roleid").val();
                    if (roleid) { $("#urrgrid").datagrid("selectRecord", roleid); }
                }
            });
        };
        //刷新列表
        var reload = function () {
            $("#urgrid").datagrid("unselectAll");
            $("#urgrid").datagrid("reload");
        };
        //按钮事件
        manage.buttonInit("basicuserrole", function (f) { return eval(f); });
    });
</script>